package com.wm.vodService.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.wm.commonutils.R;
import com.wm.commonutils.result.ResultCode;
import com.wm.servicebase.exceptionhandle.GuliException;
import com.wm.vodService.constants.InitAliyunClient;
import com.wm.vodService.constants.VodConstants;
import com.wm.vodService.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: sshdg
 * @Date: 2020/10/4 14:42
 */
@RestController
@Api(tags = "阿里云视频点播")
@RequestMapping("/eduVod/video")
//@CrossOrigin
public class VodController {
    @Autowired
    VodService vodService;

    @ApiOperation("上传视频到阿里云")
    @PostMapping("/uploadVideo")
    public R uploadOssFile(MultipartFile file){
        String videoId = vodService.uploadVideo(file);
        return R.ok().data("videoId", videoId);
    }

    @ApiOperation("根据视频id删除阿里云视频")
    @DeleteMapping("/removeAliyunVideo/{id}")
    public R removeAliyunVideo(@PathVariable String id) {

        try {
            //初始化阿里云客户端对象
            DefaultAcsClient client = InitAliyunClient.initVodClient(VodConstants.ACCESS_KEY_ID, VodConstants.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            deleteVideoRequest.setVideoIds(id);
            client.getAcsResponse(deleteVideoRequest);
            return R.ok();
        } catch (ClientException e) {
            e.printStackTrace();
            throw GuliException.from(ResultCode.DELETE_ERROR);
        }

    }

    @ApiOperation("删除多个阿里云视频")
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.deleteBatch(videoIdList);
        return R.ok();
    }

    @ApiOperation("根据视频id获取视频凭证")
    @GetMapping("/getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id ){


        try {
            //创建初始化对象
            DefaultAcsClient client = InitAliyunClient.initVodClient(VodConstants.ACCESS_KEY_ID, VodConstants.ACCESS_KEY_SECRET);

            //创建获取凭证的request和response对象
            GetVideoPlayAuthRequest authRequest = new GetVideoPlayAuthRequest();

            //向authRequest设置视频id
            authRequest.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse acsResponse = null;
            acsResponse = client.getAcsResponse(authRequest);
            String playAuth = acsResponse.getPlayAuth();

            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw GuliException.from(ResultCode.GET_PLAYAUTH_FAIL);
        }
    }
}
